Option Explicit On
Option Strict On

Imports CObject
Imports System.Data.SqlClient
Imports ConnectionServer
Imports Objects.Security.Cryptography
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory
Imports System.Reflection
Imports CObject.TestAttributes
Imports System.Configuration

Namespace EmployeeInfo


    '' -----------------------------------------------------------------------------
    ''' Project	 : Objects
    ''' Class	 : EmployeeInfo.Employee
    ''' 
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Represents an Employee as pulled from PeopleSoft
    ''' </summary>
    ''' <remarks>
    ''' </remarks>    
    ''' -----------------------------------------------------------------------------
    <Serializable()> Public Class Employee
        Inherits CObject.CObject
        Implements System.Runtime.Serialization.ISerializable
        Private mbIsOverriden As Boolean = False
        Public Sub New()
            MyBase.New()

        End Sub
        Public Sub New(ByVal psGUI As String)
            MyBase.New(psGUI)

        End Sub
        Public Sub New(ByVal piGUI As Integer)
            MyBase.New(piGUI)
        End Sub
        Public Overrides ReadOnly Property Key As String
            Get
                Return "GPN"
            End Get
        End Property
        Public Enum Source
            GUI
            GPN
            LPN
            UPN
        End Enum
        ''' <summary>
        ''' To Instantiate the Employee Object with GPN, LPN or UPN
        ''' To Support the Bcakward Comaptibility
        ''' </summary>
        ''' <param name="psSource"> Column Name</param>
        ''' <param name="psSourceID">Column Value</param>
        ''' <remarks></remarks>
        Public Sub New(ByVal psSourceID As String, ByVal psSource As Source)
            Select Case psSource
                Case Source.GUI
                    Dim loColl As CCollection = New CCollection(Me.Clone, "GUI = '" & psSourceID & "'")
                    If Not loColl.DataObject Is Nothing Then
                        mhtProperties = loColl.DataObject.Properties
                    End If
                Case Source.GPN
                    Dim loColl As CCollection = New CCollection(New CompleteEmployee, "GPN = '" & psSourceID & "'")
                    If Not loColl.DataObject Is Nothing Then
                        mhtProperties = loColl.DataObject.Properties
                    End If
                Case Source.LPN
                    Dim loColl As CCollection = New CCollection(Me.Clone, "LPN = '" & psSourceID & "'")
                    If Not loColl.DataObject Is Nothing Then
                        mhtProperties = loColl.DataObject.Properties
                    End If
                Case Source.UPN
                    Dim loColl As CCollection = New CCollection(Me.Clone, "UPN = '" & psSourceID & "'")
                    If Not loColl.DataObject Is Nothing Then
                        mhtProperties = loColl.DataObject.Properties
                    End If
            End Select

        End Sub
        ''' <summary>
        ''' Used for parsing ADAM
        ''' </summary>
        ''' <param name="poResult"></param>
        ''' <param name="psProp"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function ADProperty(ByVal poResult As SearchResult, ByVal psProp As String) As String
            Return poResult.Properties(psProp).Item(0).ToString
        End Function
        ''' <summary>
        ''' Used for parsing ADAM
        ''' </summary>
        ''' <param name="poDE"></param>
        ''' <param name="psProp"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function DEProperty(ByVal poDE As DirectoryEntry, ByVal psProp As String) As String
            Try
                Return poDE.Properties.Item(psProp).Value.ToString
            Catch
                Return "     "
            End Try
        End Function
        ''' <summary>
        ''' Specifices if the logged in user has been overriden
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property IsOverriden() As Boolean
            Get
                Return (mbIsOverriden)
            End Get
            Set(ByVal value As Boolean)
                mbIsOverriden = value
            End Set
        End Property
        ''' <summary>
        ''' Returns an encrypted GUI
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Obsolete("Use Encrypted64GUI instead", True)> Public Function EncryptedGUI() As String
            Dim loCrypt As Encryption = New Encryption()
            Return loCrypt.EncryptString(GUI)
        End Function
        ''' <summary>
        ''' Encrypts Users GUI
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Encrypted64GUI() As String
            Dim loCrypt As Encryption = New Encryption()
            Return loCrypt.EncryptString64(GUI)
        End Function
        ''' <summary>
        ''' Uses Request.LogonUserIdentity to create Employee object
        ''' </summary>
        ''' <param name="loID"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal loID As System.Security.Principal.WindowsIdentity)

            Dim lsUser As String = loID.Name.ToLower
            Dim lsDomain As String = lsUser.Substring(0, lsUser.IndexOf("\")).ToUpper
            lsUser = lsUser.Substring(lsUser.IndexOf("\") + 1)
            Dim lbTermed As Boolean = True

            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                Dim loDT As DataTable
                lsSQl = "SELECT [EmployeeID], [GUI], [GPN], [LAST_NAME], [MIDDLE_NAME], [FIRST_NAME], [PREFERRED_NAME], [Rank], [BusinessUnit], [OperatingUnit], [ManagementUnit], [SMU], [NetworkID], [NAME], [InternetEmail], [CountryCode], [EmploymentStatus]  FROM [CMT_Employee] WHERE LOWER([NetworkID]) = '" + loID.Name.ToLower + "'"
                loDT = loConn.GetDataTable(lsSQl)
                Dim loRow As DataRow
                Dim cnt As Integer = loDT.Rows.Count
                For Each loRow In loDT.Rows
                    If Not loRow("EmploymentStatus") Is "T" Then
                        lbTermed = False
                        Dim lsGUI As String = loRow("GUI").ToString()
                        Dim loColl As CCollection = New CCollection(Me.Clone, "GUI = '" & lsGUI & "'")
                        mhtProperties = loColl.DataObject.Properties
                    End If
                    Exit Sub
                Next
            End Using
            Throw New Exception("(2) " & loID.Name & " is not a valid user.")
        End Sub
        Private Sub SaveEmployeeInfo(ByVal psUser As String)
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                loConn.ExecuteScalar("EXEC EMPLOYEE_Update '" & GUI & "','" & psUser & "'")
            End Using

        End Sub
        Private Sub SaveNetworkID(ByVal psUser As String)
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                loConn.ExecuteScalar("EXEC EMPLOYEE_UpdateNetworkID '" & GUI & "','" & psUser & "'")
            End Using
        End Sub

        ''' <summary>
        ''' For adminisrative use only.  
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Sec_OverrideGUI() As String
            Get
                Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                    Dim lsGUI As Object = loConn.ExecuteScalar("EXEC SEC_Override_Select '" & NetworkID & "'")

                    If Not IsDBNull(lsGUI) AndAlso Not lsGUI Is Nothing Then
                        Return lsGUI.ToString
                    Else
                        Return GUI
                    End If
                End Using
            End Get
        End Property
        ''' <summary>
        ''' AD Path To User
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property ADPath() As String
            Get
                Return CastToString(GetProperty("ADPath"))
            End Get
        End Property

        ''' <summary>
        ''' Returns the max Contact level for this user.
        ''' </summary>
        ''' <param name="pintAppID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetMaxContactLevel(ByVal pintAppID As Integer) As Integer
            Dim lobjConnServer As ConnectionServer.Connection = New ConnectionServer.Connection(Connection.SourceDataBase.CMT)

            Dim lstrContactLevel As String
            Try
                lstrContactLevel = CStr(lobjConnServer.ExecuteScalar("SEC_MaxContactLevel " & pintAppID.ToString & ",'" & GUI & "'"))
            Catch ex As Exception
                lstrContactLevel = "0"
            End Try

            If Not lstrContactLevel Is Nothing Then
                Return CInt(lstrContactLevel)
            Else
                Return 0
            End If
        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns the department number the employee is in.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>        
        Public Property DepartmentNumber() As Long
            Get
                If IsDBNull(GetProperty("DepartmentNumber")) Then
                    Return 0
                Else
                    Return CLng(GetProperty("DepartmentNumber"))
                End If
            End Get
            Set(ByVal Value As Long)
                SetProperty("DepartmentNumber", Value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Shows the current status of the employee
        ''' </summary>
        ''' <remarks>
        ''' </remarks>       
        Public Property Status() As String
            Get
                If IsDBNull(GetProperty("Status")) Then
                    Return "No Status Available"
                Else
                    Return CStr(GetProperty("Status"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("Status", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' This will return GUI.  
        ''' </summary>
        ''' <remarks>
        ''' </remarks>       
        <Obsolete("Use GUI Instead", True)> Public Property UPN() As String
            Get
                If IsDBNull(GetProperty("UPN")) Then
                    Return "No UPN Available"
                Else
                    Return CStr(GetProperty("UPN"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("UPN", Trim(Value))
            End Set
        End Property
        ''' <summary>
        ''' The UPN of the user. (Local Personnel Number)
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property LPN() As String
            Get
                Return CastToString(GetProperty("LPN"))
            End Get
        End Property
        ''' <summary>
        ''' Global Unique ID
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property GUI() As String
            Get
                If IsDBNull(GetProperty("GUI")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("GUI"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("GUI", Trim(Value))
            End Set
        End Property

        ''' <summary>
        ''' Global Personnel number
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property GPN() As String
            Get
                Return CStr(GetProperty("GPN"))
            End Get
            Set(ByVal Value As String)
                SetProperty("GPN", Trim(Value))
            End Set

        End Property
        Public Property EmployeeID As Integer
            Get
                Return CastToInteger(GetProperty("EmployeeID"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("EmployeeID", value)
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Pulls The Name from the NotesEMail Property if No CommonName is available in the Database
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property CommonName() As String
            Get
                Dim lsCommonName As String
                lsCommonName = CastToString(GetProperty("CommonName"))

                If lsCommonName.Length = 0 Then
                    If IsDBNull(NotesEmail) Or Trim(NotesEmail).Length = 0 Then
                        Return Name
                    Else
                        If NotesEmail <> "No E-Mail Available" Then
                            Return Left(NotesEmail, InStr(NotesEmail, "/") - 1)
                        Else
                            Return Name
                        End If
                    End If
                Else
                    Return lsCommonName
                End If
            End Get
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns the PeopleSoft name of the employee
        ''' </summary>
        ''' <remarks>
        ''' </remarks>     
        Public Property Name() As String
            Get
                If IsDBNull(GetProperty("NAME")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("NAME"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("NAME", Trim(Value))
            End Set
        End Property

        Public Property FirstName() As String
            Get
                If IsDBNull(GetProperty("FIRST_NAME")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("FIRST_NAME"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("FIRST_NAME", Trim(Value))
            End Set
        End Property

        Public Property MiddleName() As String
            Get
                If IsDBNull(GetProperty("MIDDLE_NAME")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("MIDDLE_NAME"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("MIDDLE_NAME", Trim(Value))
            End Set
        End Property

        Public Property LastName() As String
            Get
                If IsDBNull(GetProperty("LAST_NAME")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("LAST_NAME"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("LAST_NAME", Trim(Value))
            End Set
        End Property

        Public Property PreferredName() As String
            Get
                If IsDBNull(GetProperty("PREFERRED_NAME")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("PREFERRED_NAME"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("PREFERRED_NAME", Trim(Value))
            End Set
        End Property

        ''' --------------------------------------------------------------------------------------------------------------
        ''' <summary>
        ''' The preffered name of the employee based on the last name, then the first name if the preffered name is empty
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        ''' <history>
        '''</history>
        ''' --------------------------------------------------------------------------------------------------------------
        Public ReadOnly Property DisplayName() As String
            Get
                Using loConn As Connection = New Connection(SourceDataBase)
                    Dim lsSQL As String = "SELECT (LAST_NAME + ', ' + CASE PREFERRED_NAME WHEN '' THEN FIRST_NAME ELSE PREFERRED_NAME END) AS PREFERRED_NAME FROM CMT_Employee WHERE GPN = '" & GPN & "'"
                    Dim loComm As SqlCommand = New SqlCommand(lsSQL, loConn.Connection)
                    Return CStr(loComm.ExecuteScalar())
                End Using
            End Get

        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Numberic Rank of the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Rank() As Integer
            Get
                If IsDBNull(GetProperty("Rank")) Then
                    Return 99
                Else
                    Return CInt(GetProperty("Rank"))
                End If
            End Get
            Set(ByVal Value As Integer)
                SetProperty("Rank", Trim(CStr(Value)))
            End Set
        End Property
        ''' <summary>
        ''' The Employee's Experience Level
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ExperienceLevel() As String
            Get
                If IsDBNull(GetProperty("ExpLevel")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("ExpLevel"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("ExpLevel", Trim(Value))
            End Set
        End Property
        ''' <summary>
        ''' Employee's Job code
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property JobCode() As String
            Get
                If IsDBNull(GetProperty("JOBCODE")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("JOBCODE"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("JOBCODE", Trim(Value))
            End Set
        End Property
        ''' <summary>
        ''' Employee's CostRate
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property CostRate() As Decimal
            Get
                If IsDBNull(GetProperty("CostRate")) Then
                    Return 0
                Else
                    Return CInt(GetProperty("CostRate"))
                End If
            End Get
            Set(ByVal Value As Decimal)
                SetProperty("CostRate", Trim(CStr(Value)))
            End Set
        End Property


       
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Description of the employee's rank
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>        
        ''' -----------------------------------------------------------------------------
        Public Property RankDescription() As String
            Get
                If IsDBNull(GetProperty("RankDescription")) Then
                    Return "No Rank Description Available"
                Else
                    Return CStr(GetProperty("RankDescription"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("RankDescription", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The BU in US### format that the employee is in
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property BusinessUnit() As String
            Get
                If IsDBNull(GetProperty("BusinessUnit")) Then
                    Return "No BusinessUnit Available"
                Else
                    Return CStr(GetProperty("BusinessUnit"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("BusinessUnit", Trim(Value))
            End Set
        End Property
        ''' <summary>
        ''' Operating Unit of user
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property OperatingUnit() As String
            Get
                If IsDBNull(GetProperty("OperatingUnit")) Then
                    Return "00000"
                Else
                    Return CStr(GetProperty("OperatingUnit"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("OperatingUnit", Trim(Value))
            End Set
        End Property

        ''' <summary>
        ''' Operating Unit Description of user
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property OperatingUnitDescription() As String
            Get
                If IsDBNull(GetProperty("OUDescription")) Then
                    Return "N/A"
                Else
                    Return CStr(GetProperty("OUDescription"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("OUDescription", Trim(Value))
            End Set
        End Property

        ''' <summary>
        ''' Complete Coding Block for employee
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property CodingBlock() As String
            Get
                Return BusinessUnit & " " & OperatingUnit & " " & ManagementUnit & " " & SMU
            End Get
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Description of the BU The employee is in
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property BUDescription() As String
            Get
                If IsDBNull(GetProperty("BUDescription")) Then
                    Return "No BusinessUnit Available"
                Else
                    Return CStr(GetProperty("BUDescription"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("BUDescription", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Description of the MU the employee is in
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property MUDescription() As String
            Get
                If IsDBNull(GetProperty("MUDescription")) Then
                    Return "No ManagementUnit Available"
                Else
                    Return CStr(GetProperty("MUDescription"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("MUDescription", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The numeric value of the MU the employee is in
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property ManagementUnit() As String
            Get
                If IsDBNull(GetProperty("ManagementUnit")) Then
                    Return "No ManagementUnit Available"
                Else
                    Return CStr(GetProperty("ManagementUnit"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("ManagementUnit", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Employee's physical Location
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Location() As String
            Get
                If IsDBNull(GetProperty("LOCATION")) Then
                    Return "No Location Available"
                Else
                    Return CStr(GetProperty("LOCATION"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("LOCATION", Trim(Value))
            End Set
        End Property
        ''' <summary>
        ''' The Employee's Tax Location
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TaxLocation() As String
            Get
                If IsDBNull(GetProperty("TAX_LOCATION")) Then
                    Return "No Tax Location Available"
                Else
                    Return CStr(GetProperty("TAX_LOCATION"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("TAX_LOCATION", Trim(Value))
            End Set
        End Property


        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The employee's bill rate 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property BillRate() As Decimal
            Get
                If IsDBNull(GetProperty("BillRate")) Then
                    Return 0
                Else
                    Return CDec(GetProperty("BillRate"))
                End If
            End Get
            Set(ByVal value As Decimal)
                SetProperty("BillRate", value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The employee's standard hours
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property StandardHours() As Decimal
            Get
                If IsDBNull(GetProperty("StandardHours")) Then
                    Return 0
                Else
                    Return CDec(GetProperty("StandardHours"))
                End If
            End Get
            Set(ByVal value As Decimal)
                SetProperty("StandardHours", value)
            End Set
        End Property


        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The employee's exemption status
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Exempt() As String
            Get

                Return CastToString(GetProperty("Exempt"))

            End Get
            Set(ByVal value As String)
                SetProperty("Exempt", value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Building the employee is in 
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Building() As String
            Get
                If IsDBNull(GetProperty("Building")) Then
                    Return "No Building Available"
                Else
                    Return CStr(GetProperty("Building"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("Building", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Address for the employee if any
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property Address1() As String
            Get
                If IsDBNull(GetProperty("ADDRESS1")) Then
                    Return "No Address Available"
                Else
                    Return CStr(GetProperty("ADDRESS1"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("ADDRESS1", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Address of the employee if any
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Address2() As String
            Get
                If IsDBNull(GetProperty("ADDRESS2")) Then
                    Return ""
                Else
                    Return CStr(GetProperty("ADDRESS2"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("ADDRESS2", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Mailing City for the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property City() As String
            Get
                If IsDBNull(GetProperty("CITY")) Then
                    Return "No City Available"
                Else
                    Return CStr(GetProperty("CITY"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("CITY", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Mailing State of the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        <TestAttributes.TestColumnName("STATE")> Public Property PostalState() As String
            Get
                If IsDBNull(GetProperty("STATE")) Then
                    Return "No State Available"
                Else
                    Return CStr(GetProperty("STATE"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("STATE", Value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Phone number for the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>        
        ''' -----------------------------------------------------------------------------
        Public Property Phone() As String
            Get
                If IsDBNull(GetProperty("PHONE")) Then
                    Return "No Phone Available"
                Else
                    Return CStr(GetProperty("PHONE"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("PHONE", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Employee's Mailing ZIP code
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property ZipCode() As String
            Get
                If IsDBNull(GetProperty("Postal")) Then
                    Return "No ZipCode Available"
                Else
                    Return CStr(GetProperty("Postal"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("Postal", Trim(Value))
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Employee's Lotus Notes Email
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property NotesEmail() As String
            Get
                If IsDBNull(GetProperty("NotesEmail")) Then
                    Return "No E-Mail Available"
                Else
                    Return CStr(GetProperty("NotesEmail"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("NotesEmail", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The GUI of the Employee's supervisor
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>    
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property ReportsTo() As String
            Get
                Return CastToString(GetProperty("ReportsTo"))
            End Get
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The GUI of the Employee's counselor
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------Public ReadOnly Property ReportsTo() As String
        Public ReadOnly Property Counselor() As String
            Get
                Return CastToString(GetProperty("Counselor"))
            End Get
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' An arraylist of the Objects representing the employee's direct reports
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property DirectReports() As CDataTable
            Get
                Dim loColl As CCollection = New CCollection(New Employee, "ReportsTo = '" & GUI & "'")
                Return loColl.CDatatable
            End Get
        End Property

        Public ReadOnly Property DirectReportsDT() As DataTable
            Get
                Dim loColl As CCollection = New CCollection(New Employee, "ReportsTo = '" & GUI & "'", True)
                Return loColl.DataTable
            End Get
        End Property

        Public ReadOnly Property Counsels() As CDataTable
            Get
                Dim loColl As CCollection = New CCollection(New Employee, "Counseler = '" & GUI & "'")
                Return loColl.CDatatable
            End Get
        End Property

        Public ReadOnly Property CounselsDT() As DataTable
            Get
                Dim loColl As CCollection = New CCollection(New Employee, "Counseler = '" & GUI & "'", True)
                Return loColl.DataTable
            End Get
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' A datatable containing the NAME and GUI  representing the list of direct and indirect reports
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Function AllReports() As DataTable
            Dim loBusInfo As BusinessInfo = New BusinessInfo
            Dim loDT As DataTable = New DataTable
            Dim loReturn As DataTable = New DataTable
            Dim loDR As DataRow
            Dim loDS As DataSet = loBusInfo.GetOrgChart(GUI, False)
            loReturn.Columns.Add("Name")
            loReturn.Columns.Add("GUI")
            For Each loDT In loDS.Tables
                For Each loDR In loDT.Rows
                    Dim loRet As DataRow = loReturn.NewRow
                    loRet("Name") = loDR("CommonName")
                    loRet("GUI") = loDR("Child")
                    loReturn.Rows.Add(loRet)
                Next
            Next

            Return loReturn

        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Description of the SMU of this employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>        
        ''' -----------------------------------------------------------------------------
        Public Property SMUDescription() As String
            Get
                If IsDBNull(GetProperty("SMUDescription")) Then
                    Return "No SMU Available"
                Else
                    Return CStr(GetProperty("SMUDescription"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("SMUDescription", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Numeric value of the Employees SMU
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property SMU() As String
            Get
                If IsDBNull(GetProperty("SMU")) Then
                    Return "No SMU Available"
                Else
                    Return CStr(GetProperty("SMU"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("SMU", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Internet E-Mail. 
        ''' </summary>
        ''' <value></value>
        ''' <remarks> Used for sending employee notices via SMTP
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property InternetEmail() As String
            Get
                If IsDBNull(GetProperty("InternetEmail")) Then
                    Return "No E-Mail Available"
                Else
                    Return CStr(GetProperty("InternetEmail"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("InternetEmail", Trim(Value))
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Access ID assigned to the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property AccessID() As String
            Get
                Return CastToString(GetProperty("access_id"))
            End Get
            Set(ByVal Value As String)
                SetProperty("access_id", Trim(Value))
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Scorecard Comments is a comment as to why an employee is not scorecarded
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Scorecard_Comment() As String
            Get
                Return CastToString(GetProperty("scorecarded_comment"))
            End Get
            Set(ByVal Value As String)
                SetProperty("scorecarded_comment", Trim(Value))
            End Set
        End Property


        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns True if the employee is Scorecarded
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property Is_Scorecarded() As Boolean
            Get
                Return CastToBoolean(GetProperty("scorecarded"))
            End Get
            Set(ByVal Value As Boolean)
                SetProperty("scorecarded", Value)
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The People Soft name of the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>        
        ''' -----------------------------------------------------------------------------
        Public Property PersonnelName() As String
            Get
                If IsDBNull(GetProperty("PersonnelName")) Then
                    Return "No PersonnelName Available"
                Else
                    Return CStr(GetProperty("PersonnelName"))
                End If
            End Get
            Set(ByVal Value As String)
                SetProperty("PersonnelName", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Last date which the employee was hired
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property HireDate() As Date
            Get
                If Not IsDBNull(GetProperty("HireDate")) Then
                    Return CDate(GetProperty("HireDate"))
                End If
            End Get
            Set(ByVal Value As Date)
                SetProperty("HireDate", Value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns True If the employee is subject Scorecards
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property HasScoreCard() As Boolean
            Get
                Return CastToBoolean(GetProperty("HasScoreCard"))
            End Get
            Set(ByVal Value As Boolean)
                SetProperty("HasScoreCard", Value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The date the employee terminated
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>
        ''' -----------------------------------------------------------------------------
        Public Property TerminationDate() As Date
            Get
                If Not IsDBNull(GetProperty("TerminationDate")) Then
                    Return CDate(CStr(GetProperty("TerminationDate")))
                End If
            End Get
            Set(ByVal Value As Date)
                SetProperty("TerminationDate", Value)
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns a CDatatable with this person's direct reports
        ''' </summary>
        ''' <returns>CDataTable</returns>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Function GetDirectReports() As CObject.CDataTable
            Dim Coll As CObject.CCollection = New CObject.CCollection(New Employee, " ReportsTo = '" & GUI & "'")
            Return Coll.CDatatable
        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Employee's 5+2
        ''' </summary>
        ''' <value></value>
        ''' <remarks>This data only available for US205 employee's
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public Property NetworkID() As String
            Get
                Return CastToString(GetProperty("NetworkID"))
            End Get
            Set(ByVal Value As String)
                SetProperty("NetworkID", Trim(Value))
            End Set
        End Property
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Service Date is the original hired date for the employee
        ''' </summary>
        ''' <value></value>
        ''' <remarks>This is the date used for figureing out Anniverseries
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property ServiceDate() As Date
            Get
                Return CastToDate(GetProperty("ServiceDate"))
            End Get
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' My Department ID
        ''' </summary>
        ''' <value></value>
        ''' <remarks>My Department changes 
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property Department_ID() As String
            Get
                If IsDBNull(GetProperty("Department_ID")) Then
                    Return "N/A"
                Else
                    Return CStr(GetProperty("Department_ID"))
                End If
            End Get
        End Property

        Public ReadOnly Property DEPARTMENT_DESCR() As String
            Get
                If IsDBNull(GetProperty("DEPARTMENT_DESCR")) Then
                    Return "N/A"
                Else
                    Return CStr(GetProperty("DEPARTMENT_DESCR"))
                End If
            End Get

        End Property
        Public ReadOnly Property SETID_Dept() As String
            Get
                If IsDBNull(GetProperty("SETID_Dept")) Then
                    Return "N/A"
                Else
                    Return CStr(GetProperty("SETID_Dept"))
                End If
            End Get
        End Property


        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String

            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "Select * from CMT_Employee WITH (NOLOCK) "
                Case CommandType.SQLInsert
                    Return "INSERT INTO CMT_Employee(GUI,GPN,LAST_NAME,MIDDLE_NAME,FIRST_NAME,PREFERRED_NAME,Rank,BusinessUnit,OperatingUnit,ManagementUnit,SMU,NetworkID,Name) VALUES ({GUI},{GPN},{LAST_NAME},{MIDDLE_NAME},{FIRST_NAME},{PREFERRED_NAME},{Rank},{BusinessUnit},{OperatingUnit},{ManagementUnit},{SMU},{NetworkID},{NAME})"
                Case CommandType.SQLUpdate
                    Return "UPDATE CMT_Employee SET GUI = {GUI}, GPN = {GPN}, LAST_NAME = {LAST_NAME}, MIDDLE_NAME = {MIDDLE_NAME}, FIRST_NAME = {FIRST_NAME}, PREFERRED_NAME = {PREFERRED_NAME}, Rank = {Rank}, BusinessUnit = {BusinessUnit}, OperatingUnit = {OperatingUnit}, ManagementUnit = {ManagementUnit}, SMU = {SMU}, NetworkID = {NetworkID}, NAME = {Name} WHERE EmployeeID = {EmployeeID}"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select

        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Description as it appears on the Org Chart
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public Property OrgChartDescription() As String
            Get
                Return CStr(GetProperty("JobDescription"))
            End Get
            Set(ByVal Value As String)
                SetProperty("JobDescription", Value)
            End Set
        End Property

        Public Overrides Function Clone() As CObject.CObject
            Return New Employee
        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Used to update some fields of the employee by employee_maintenance.aspx
        ''' </summary>
        ''' <param name="pstrNACTitle"></param>
        ''' <param name="pstrBUResponsibility"></param>
        ''' <param name="pstrBio"></param>
        ''' <remarks>
        ''' </remarks>     
        ''' -----------------------------------------------------------------------------
        Public Sub UpdateEmployeeDetails(ByVal pstrNACTitle As String, ByVal pstrBUResponsibility As String, ByVal pstrBio As String)
            Dim lobjConnServer As ConnectionServer.Connection = New ConnectionServer.Connection(Connection.SourceDataBase.CMT)

            Dim lstrSQL As String = "EXEC USER_UpdateEmployeeDetails '" & GUI & "','" & FixSQLString(pstrNACTitle) & "','" & FixSQLString(pstrBUResponsibility) & "','" & FixSQLString(pstrBio) & "'"

            lobjConnServer.ExecuteScalar(lstrSQL)

        End Sub
        Private Function FixSQLString(ByVal pstrValue As String) As String
            Return Replace(pstrValue, "'", "''")
        End Function

        Public Overrides ReadOnly Property SourceDataBase() As ConnectionServer.Connection.SourceDataBase
            Get
                Return ConnectionServer.Connection.SourceDataBase.CMT
            End Get
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The Security Groups in which the employee is a member
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>      
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property Roles() As DataTable
            Get
                Dim loConnection As Connection = New Connection(Connection.SourceDataBase.CMT)


                Return loConnection.GetDataTable("EXEC SEC_GetUserRoles '" & GUI & "'")


            End Get
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The All possible Security Groups in which the employee is a member
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' How is it different from Roles?
        ''' Roles give the list of roles that are assigned to Secure Objects
        ''' AllRoles gives the list of all possible roles the user is in (irrespective of Secure Objects assigned or not)
        ''' </remarks>       
        ''' -----------------------------------------------------------------------------
        Public ReadOnly Property AllRoles() As DataTable
            Get
                Dim loConnection As Connection = New Connection(Connection.SourceDataBase.CMT)
                Return loConnection.GetDataTable("EXEC SEC_GetAllUserRoles '" & GUI & "')")

            End Get
        End Property
        ''' <summary>
        ''' The roles the user can apply for
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property SecurityDefaultExceptions() As DataSet
            Get
                Dim loConnection As Connection = New Connection(Connection.SourceDataBase.CMT)




                Dim loConn As SqlClient.SqlConnection = loConnection.Connection


                Dim loComm As SqlCommand = New SqlCommand("EXEC dbo.SEC_GetDefaultExceptions '" & GUI & "',0", loConn)

                Dim loDataSet As DataSet = New DataSet
                Dim loTable As DataTable
                Dim llTableCount As Integer
                Dim loRelation As DataRelation
                Dim loRow As DataRow

                Dim loTableMaster As DataTable

                Dim loAdp As SqlDataAdapter = New SqlDataAdapter(loComm)
                Try
                    loAdp.Fill(loDataSet)
                Finally
                    loConn.Close()
                End Try
                For Each loTableMaster In loDataSet.Tables
                    loTableMaster.TableName = llTableCount.ToString
                    llTableCount += 1
                    For Each loRow In loTableMaster.Rows
                        For Each loTable In loDataSet.Tables
                            If loTable.TableName <> loTableMaster.TableName Then
                                If loTable.Rows.Count > 0 Then
                                    If Not IsDBNull(loTable.Rows(0)("parentId")) Then
                                        If loRow("applicationId").ToString = loTable.Rows(0)("ParentID").ToString Then
                                            loRelation = New DataRelation(loRow("ApplicationName").ToString, loTableMaster.Columns("ApplicationID"), loTable.Columns("ParentID"))
                                            Try
                                                loDataSet.Relations.Add(loRelation)
                                            Catch ex As Exception
                                            End Try
                                        End If
                                    End If
                                End If
                            End If
                        Next
                    Next
                Next
                Return loDataSet
            End Get
        End Property


        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns true if the employee is a member of the passed Group Name
        ''' </summary>
        ''' <param name="pstrRole"></param>
        ''' <returns></returns>
        ''' <remarks>
        ''' </remarks>       
        '''     ' Depreciated
        ''' -----------------------------------------------------------------------------
        Public Function IsMember(ByVal pstrRole As String) As Boolean
            Dim lobjConnServer As ConnectionServer.Connection = New ConnectionServer.Connection(Connection.SourceDataBase.CMT)
            Dim lbooIsMember As Boolean
            Dim lsSQL As String = String.Empty
            Try
                lsSQL = "EXEC EMPLOYEE_IsMember '" & GUI & "','" & pstrRole & "'"
                lbooIsMember = CBool(lobjConnServer.ExecuteScalar(lsSQL))
            Catch ex As Exception
                lbooIsMember = False
            End Try
            Return lbooIsMember

        End Function
        ''' <summary>
        ''' Returns true if the employee is a member of the passed Group ID
        ''' </summary>
        ''' <param name="piRoleID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function IsMember(ByVal piRoleID As Integer) As Boolean
            Dim lobjConnServer As ConnectionServer.Connection = New ConnectionServer.Connection(Connection.SourceDataBase.CMT)
            Dim lbooIsMember As Boolean
            Dim lsSQL As String = String.Empty
            Try
                lsSQL = "EXEC EMPLOYEE_IsMemberByID '" & GUI & "'," & piRoleID.ToString
                lbooIsMember = CBool(lobjConnServer.ExecuteScalar(lsSQL))
            Catch ex As Exception
                lbooIsMember = False
            End Try
            Return lbooIsMember
        End Function
        ''' <summary>
        ''' Method to get the Employee details.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetEmployeeInfo() As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                Dim loDT As DataTable
                lsSQl = "SELECT EmployeeID, GUI, GPN, LAST_NAME, MIDDLE_NAME, FIRST_NAME, PREFERRED_NAME, Rank, BusinessUnit, OperatingUnit, ManagementUnit, SMU, NetworkID, Name FROM CMT_Employee"
                loDT = loConn.GetDataTable(lsSQl)
                Return loDT
            End Using
        End Function
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Returns a relations dataset with the ObjectID's of the objects on the page in which the user has permission to
        ''' </summary>
        ''' <param name="psPagePath"></param>
        ''' <returns></returns>
        ''' <remarks>
        ''' </remarks>
        ''' -----------------------------------------------------------------------------
        Public Function GetUserPermissions(ByVal psPagePath As String) As DataSet
            Dim loConnServer As ConnectionServer.Connection = New ConnectionServer.Connection
            Using loConn As SqlClient.SqlConnection = CType(loConnServer.GetConnection(ConnectionServer.Connection.SourceDataBase.CMT), SqlConnection)
                Dim loComm As SqlCommand = New SqlCommand("EXEC dbo.SEC_GetUserPermissionsTable '" & GUI & "','" & psPagePath & "'", loConn)
                Dim loDataSet As DataSet = New DataSet
                Dim loTable As DataTable
                Dim llTableCount As Integer
                Dim loRelation As DataRelation
                Dim loRow As DataRow
                Dim loRawTable As DataTable = New DataTable(llTableCount.ToString)
                Dim loTableMaster As DataTable

                Dim loAdp As SqlDataAdapter = New SqlDataAdapter(loComm)
                Try
                    loAdp.Fill(loRawTable)
                Finally
                    loConn.Close()
                End Try
                For Each ldrRaw As DataRow In loRawTable.Rows
                    ' If ldrRaw("Name").ToString.Trim.ToLower <> psPagePath.Trim.ToLower Then
                    If loDataSet.Tables.IndexOf(ldrRaw("Parent").ToString) = -1 Then
                        Dim loNewTable As DataTable = New DataTable(ldrRaw("Parent").ToString)
                        loNewTable.Columns.AddRange(New DataColumn() _
                                {New DataColumn("ObjectID"), New DataColumn("Name"), _
                                New DataColumn("Description"), New DataColumn("HRef"), _
                                New DataColumn("Parent"), New DataColumn("Type"), _
                                New DataColumn("Target"), New DataColumn("ApplicationID"), _
                                New DataColumn("Sort"), New DataColumn("MSort")})
                        loDataSet.Tables.Add(loNewTable)
                    End If
                    loTable = loDataSet.Tables(ldrRaw("Parent").ToString)
                    Dim ldrNew As DataRow = loTable.NewRow
                    ldrNew.ItemArray = ldrRaw.ItemArray
                    loTable.Rows.Add(ldrNew)
                    'End If
                Next

                For Each loTableMaster In loDataSet.Tables
                    loTableMaster.TableName = llTableCount.ToString
                    llTableCount += 1
                    For Each loRow In loTableMaster.Rows
                        For Each loTable In loDataSet.Tables
                            If loTable.TableName <> loTableMaster.TableName Then
                                If loTable.Rows.Count > 0 Then
                                    If Not IsDBNull(loTable.Rows(0)("Parent")) Then
                                        If loRow("ObjectID").ToString = loTable.Rows(0)("Parent").ToString Then
                                            loRelation = New DataRelation(loRow("Name").ToString, loTableMaster.Columns("ObjectID"), loTable.Columns("Parent"))
                                            Try
                                                loDataSet.Relations.Add(loRelation)
                                            Catch ex As Exception
                                            End Try
                                        End If
                                    End If
                                End If
                            End If
                        Next
                    Next
                Next
                Return loDataSet
            End Using
        End Function


        ''' <summary>
        ''' Returns the Countries list that the user has access to
        ''' Considering there will be a Secure Group for every Country (Managed Entity)
        ''' </summary>
        ''' <returns>Datatable</returns>
        Public Function Countries() As DataTable
            Dim loDT As DataTable = New DataTable
            Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
            Dim lsSql As String = "EXEC EMPLOYEE_GetCountries '" & GUI & "'"
            loDT = loConn.GetDataTable(lsSql)
            loConn.Connection.Close()
            Return loDT
            'cannot use role object because need to inner join with sec_user_rights. -VATTIMY
        End Function
        ''' <summary>
        ''' Returns the processing ( GFIS or Managerial) countries that are configured for the Infoweb Countries 
        ''' that the user has access to
        ''' </summary>
        ''' <returns>Datatable</returns>
        Public Function ProcessingCountries() As DataTable

            Dim loDT As DataTable = New DataTable
            Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
            Dim lsSql As String = "EXEC EMPLOYEE_GetProcessingCountries '" & GUI & "'"

            loDT = loConn.GetDataTable(lsSql)
            loConn.Connection.Close()
            Return loDT
        End Function


        ''' <summary>
        ''' Returns All the available Profiles for the employee (GUI)
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Profiles() As DataTable
            Dim loDT As DataTable = New DataTable
            Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
            Dim lsSQL As String = "EXEC EMPLOYEE_Profiles '" & GUI & "' "
            loDT = loConn.GetDataTable(lsSQL)
            loConn.Connection.Close()
            Return loDT

        End Function

        ''' <summary>
        ''' Returns the Roles the current User has default rights to
        ''' </summary>
        ''' <param name="piApplicationID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function RolesByApplication(ByVal piApplicationID As Integer) As DataTable
            Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
            Return loConn.GetDataTable("EXEC SEC_GetApplicationExceptionRoles " & piApplicationID.ToString & ",'" & GUI & "'")
        End Function

        Public Function RolesByApplicationRecursive(ByVal piApplicationID As Integer) As DataTable
            Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
            Return loConn.GetDataTable("EXEC SEC_GetApplicationExceptionRolesRecursive " & piApplicationID.ToString & ",'" & GUI & "'")
        End Function

        Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
            Dim MyType As Type = Assembly.GetAssembly(Me.GetType).GetType("Objects.EmployeeInfo.Employee")
            Dim MyMembers() As MemberInfo = MyType.GetMembers
            Dim MyMember As MemberInfo
            For Each MyMember In MyMembers
                If MyMember.MemberType = MemberTypes.Property And Me.Properties.ContainsKey(MyMember.Name) Then
                    info.AddValue(MyMember.Name, CType(Me.Properties(MyMember.Name), CData).NewValue, CType(MyMember, PropertyInfo).PropertyType)
                End If
            Next
        End Sub

        ''' <summary>
        ''' Returns true if the employee is a field administrator
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function IsFieldAdministrator(ByVal gui As String) As Boolean
            Dim lobjConnServer As ConnectionServer.Connection = New ConnectionServer.Connection(Connection.SourceDataBase.CMT)
            Dim lbooIsFieldAdmin As Boolean
            Dim lsSQL As String = String.Empty
            Try
                lsSQL = "EXEC IsFieldAdministrator '" & gui & "'"
                lbooIsFieldAdmin = CBool(lobjConnServer.ExecuteScalar(lsSQL))
            Catch ex As Exception
                lbooIsFieldAdmin = False
            End Try
            Return lbooIsFieldAdmin
        End Function
    End Class


    Public Class CompleteEmployee
        Inherits Employee

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "SELECT * FROM CMT_Employee"
                Case Else
                    Throw New Exception("CompleteEmployee is Read only")

            End Select
        End Function


        Public Sub New()
            MyBase.New()
        End Sub
        Public Sub New(ByVal GPN As String)
            Dim loColl As CCollection = New CCollection(New CompleteEmployee, "GPN='" & GPN & "'")

            Try
                Properties = CType(loColl.DataObject, Employee).Properties
            Catch e As Exception
                Properties = Nothing
            Finally

            End Try

        End Sub

#Region "Properties"

        Public ReadOnly Property GPNs() As DataTable
            Get
                Dim loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Return loConn.GetDataTable("EXEC EMPLOYEE_GPNs '" & GUI & "'")
            End Get
        End Property

#End Region

    End Class



End Namespace
